package fvortex.flute;

import fvortex.flute.client.KeyRegistry;
import fvortex.flute.client.gui.GuiContainerFlute;
import fvortex.flute.common.item.ItemFlute;
import fvortex.flute.common.util.music.*;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.input.Keyboard;

public class EventRegistry {
    public EventRegistry()
    {
        MinecraftForge.EVENT_BUS.register(this);
    }

    /**
     * When users opening gui of flute, this will work
     *  To accept keyboard inputs to play notes in free mode
     */
    @SideOnly(Side.CLIENT)
    @SubscribeEvent
    public void onFluteGuiKeyDown(GuiScreenEvent.KeyboardInputEvent event)
    {
        if (event.getGui() instanceof GuiContainerFlute) {
            World world = event.getGui().mc.world;
            EntityPlayer player = event.getGui().mc.player;
            ItemStack items = player.getHeldItem(EnumHand.MAIN_HAND);
            ItemFlute flute;
            if (items.getItem() instanceof ItemFlute){
                flute = (ItemFlute) items.getItem();
            }else if ((items = player.getHeldItem(EnumHand.OFF_HAND)).getItem() instanceof ItemFlute)
            {
                flute = (ItemFlute) items.getItem();
            }else {
                flute = null;
            }
            if (flute!=null) {
                for (int i = 0; i < KeyRegistry.NOTE_NUMBER; ++i) {
                    if (Keyboard.isKeyDown(KeyRegistry.fluteNote[i].getKeyCode())) {
                        flute.playNote(world, player, new Note(false, Note.NOTE_DURA_QUARTER, Pitch.NOTES7[i]));
                    }
                }
            }
        }
    }

}
